const ProductService = require("../../services/admin/ProductService")

const ProductController = {
  add: async (req, res) => {
    const cover = req.file ? `/productuploads/${req.file.filename}`
      : ""
    const { title, introduction, detail, userid } = req.body
    await ProductService.add({
      title, introduction, detail, userid, cover, editTime: new Date()
    })
    res.send({
      ActionType: 'ok'
    })
  },
  getList: async (req, res) => {
    console.log(req.params)
    const result = await ProductService.getList({ _id: req.params.id })
    res.send({
      ActionType: 'ok',
      data: result
    })
  },
  delList: async (req, res) => {
    await ProductService.delList({ _id: req.params.id })
    res.send({
      ActionType: 'ok',
    })
  },
  // publish: async (req, res) => {
  //   await NewsService.publish({
  //     ...req.body,
  //     editTime: new Date()
  //   })
  //   res.send({
  //     ActionType: 'ok',
  //   })
  // },
  updateList: async (req, res) => {
    const cover = req.file ? `/productuploads/${req.file.filename}`
      : ""
    const { title, introduction, detail, _id } = req.body
    await ProductService.updateList({
      title, introduction, detail, cover, editTime: new Date(), _id
    })
    res.send({
      ActionType: 'ok'
    })
  }
}
module.exports = ProductController